package com.example.pregnant.activity;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.pregnant.R;
import com.example.pregnant.Settings;

public class PregnantCalendar extends Activity implements OnItemSelectedListener{
    private int mYear;
    private int mMonth;
    private int mDay;
    Spinner spinner;
	DatePicker datePicker;
	Button btSubmit;
	
	String[] weekage;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Settings.mContext = getApplicationContext();
        
        boolean isStarted = PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Settings.IS_STARTED, false);
        if (isStarted){
        	startHomeScreen();
        }
     	   
        weekage = getResources().getStringArray(R.array.weekage);
        spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.weekage, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);
        spinner.setSelection(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getInt(Settings.WEEK_AGE, 1)-1, true);
        
        
        btSubmit = (Button) findViewById(R.id.btOk);
        btSubmit.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Settings.saveWeekAges();
				startHomeScreen();
			}
		});
        spinner.performClick();
//        spinner.set
//        btSubmit.setOnClickListener(this);
        
      
    }
    
    public void startHomeScreen(){
    	
    	Intent intent = new Intent(getApplicationContext(), HomeScreen.class);
    	startActivity(intent);
    	finish();
    }

	@Override
	public void onItemSelected(AdapterView<?> parent,View view, int pos, long id) {
//		Toast.makeText(getApplicationContext(), weekage[pos] , 1000).show();
		
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
		Editor editor = settings.edit();
		editor.putBoolean(Settings.IS_STARTED, true);
		editor.putInt(Settings.WEEK_AGE, pos+1);
		editor.commit();
		
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		
	}
	
	
//	@Override
//	public void onDateSet(DatePicker view, int year, int monthOfYear,
//			int dayOfMonth) {
//		monthOfYear += 1; //month start from 0
//		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
//		Editor editor = settings.edit();
//		editor.putBoolean(Settings.IS_STARTED, true);
//		editor.putInt(Settings.START_DAY, dayOfMonth);
//		editor.putInt(Settings.START_MONTH, monthOfYear);
//		editor.putInt(Settings.START_YEAR, year);
//		editor.commit();
//		
//		String str = year + "-" + monthOfYear + "-" + dayOfMonth; 
//		Toast.makeText(getApplicationContext(), str, 1000).show();
//		startHomeScreen();
////		Toast.makeText(getApplicationContext(), "Saved!", 1000).show();
//	}
	
//	public void showdDatePickerDialog(){
//		final Calendar c = Calendar.getInstance();
//        mYear = c.get(Calendar.YEAR);
//        mMonth = c.get(Calendar.MONTH);
//        mDay = c.get(Calendar.DAY_OF_MONTH);
//
//        new DatePickerDialog(this, 3, this, mYear, mMonth, mDay).show();
//	}
}